package com.elcom.roaming.module.synthesis.lb;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.slf4j.Logger;

import com.elcom.roaming.common.StringCouple;
import com.elcom.roaming.common.config.ConfigModule;
import com.elcom.roaming.module.synthesis.lb.client.LBClient;

public class OutputService {
	private static final int CLIENT_NUMBER = 2;
	
	private BlockingQueue<StringCouple> queue;
	private String queueName;
	private Logger logger;
	
	public OutputService(Logger logger) {
		this.logger = logger;
		this.queue = new LinkedBlockingQueue<StringCouple>();
	}
	
	public void put(StringCouple couple) {
		if (queue.size() >= ConfigModule.LB_CLIENT_MAX_QUEUE_SIZE) {
			logger.info("| Status : REJECTED - Queue is full | " + couple.toString());
		} else {
			queue.add(couple);
		}
	}
	
	public void startClients(){
		for (int i=0; i<CLIENT_NUMBER; i++) {
			LBClient client = new LBClient(logger, i, ConfigModule.LB_HOST, ConfigModule.LB_PORT, queue);
			new Thread(client, "lb-client-" + i).start();
		}
	}

	public String getQueueName() {
		return queueName;
	}

	public void setQueueName(String queueName) {
		this.queueName = queueName;
	}

	public BlockingQueue<StringCouple> getQueue() {
		return queue;
	}
	
}
